// replicating figure B2 in the appendix


******************************
**  PRELIMINARY OPERATIONS
******************************
quietly {
******************************
** Lagged values
******************************
use "data/table1/enricoall2.dta", clear
sort state district year
by state district year: g n = _n
keep if n<=2

replace year = year +2
g lagged = realada

	// Matteo adds controls
	recode sex (1=1) (2=0), g(male)
	tab lastoccup, g(doccup)
	tab collegeattend, g(dcoll)
	tab milit, g(dmil)
	tab second, g(dedu)
	global contr male relatives_shrt age ///
	doccup1 doccup2 doccup3 doccup4 doccup5 doccup6 doccup7 ///
	dcoll1 dcoll2 dcoll3 dcoll4 ///
	dmil1 dmil2 dmil3 ///
	dedu1 dedu2 dedu3
	foreach var in $contr {
	g lag_`var'=`var'
	}
keep state district year n lagged lag_*
sort state district year n
save tmp, replace

******************************
** Simultaneous values
******************************
use "data/table1/enricoall2.dta", clear
sort state district year
by state district year: g n = _n
keep if n<=2

drop lagada*
sort state district year n
merge state district year n using tmp
rm tmp.dta

* drop if last time was a redistricting year
drop if year ==1952 | year==1962 | year==1972 | year==1982 | year==1992
*keep if lagdemvoteshare>.48 & lagdemvoteshare<.52
drop democrat
g       democrat = 1 if demvoteshare>=.5
replace democrat = 0 if demvoteshare<.5

g       lagdemocrat = 1 if lagdemvoteshare >=.5
replace lagdemocrat = 0 if lagdemvoteshare <.5
g    score = realada
g lagscore = lagged
egen id = group(state district year n)
keep if score ~=. & lagscore ~=. | year==1946
keep if democrat~=. & lagdemocrat ~=. | year==1946
keep if id ~=.
replace lagdemocrat=. if year==1946

********************************
** Matteo adds Majority Status
********************************
g dem_maj = 1
replace dem_maj = 0 if congress==80 | congress==83
g majority = .
replace majority = democrat if dem_maj==1
replace majority = 1 - democrat if dem_maj==0
g lagdem_maj = 1
replace lagdem_maj = 0 if congress==81 | congress==84
replace lagdem_maj = . if congress==80
g lagmajority = .
replace lagmajority = lagdemocrat if lagdem_maj==1
replace lagmajority = 1 - lagdemocrat if lagdem_maj==0

********************************
** Matteo adds Time dummies
********************************
quietly: tab congress, g(dcon)

********************************
** Matteo adds Running variable
********************************
g dgap = demvoteshare-0.5
g lagdgap = lagdemvoteshare-0.5
g rdgap = dgap * democrat
g lagrdgap = lagdgap * lagdemocrat
g dgap2=dgap*dgap
g lagdgap2=lagdgap*lagdgap
g rdgap2=rdgap*rdgap
g lagrdgap2=lagrdgap*lagrdgap

********************************
** Matteo adds Contemporaneous Controls
********************************
recode sex (1=1) (2=0), g(male)
tab lastoccup, g(doccup)
tab collegeattend, g(dcoll)
tab milit, g(dmil)
tab second, g(dedu)

********************************
** Matteo rename vars shorter
********************************
g d = democrat
g lagd=lagdemocrat
g m = majority
g lagm = lagmajority
g dm = dem_maj
g lagdm = lagdem_maj
g inter_d_m = d*m

}
******************************
** END PRELIMINARY OPERATIONS
******************************


******************************
** REPLICATING FIGURE B2 
******************************

eststo clear

// Settings
global kernel uniform

******************************
* 1) score lagdemocrat
******************************
global y score
label variable $y "RC(t+1)" 
global run lagdgap
global run_reg lagdgap lagrdgap 

quietly {

 rdbwselect $y $run, kernel($kernel) 
	scalar cct=e(h_mserd)
	forval i = 5(1)20 {
	scalar h`i'=`i'*(0.065)/10
	g bw`i'=0
	replace bw`i'=1 if $run > -`=h`i'' & $run <`=h`i''
	} 
	
	forval i = 5(1)20 {
		reg $y lagd $run_reg if bw`i'==1, cluster(id) // model A black
			scalar bA`i'= _b[lagd]
			scalar seA`i'= _se[lagd]
		reg $y lagd lagm $run_reg if bw`i'==1, cluster(id) // model B red
			scalar bB`i'= _b[lagd]
			scalar seB`i'= _se[lagd]
		reg $y lagd lagm lagdm $run_reg if bw`i'==1, cluster(id) // model C green
			scalar bC`i'= _b[lagd]
			scalar seC`i'= _se[lagd]
		reg $y lagd lagm dcon* $run_reg if bw`i'==1, cluster(id) // model D  red
			scalar bD`i'= _b[lagd]
			scalar seD`i'= _se[lagd]
		
		matrix a`i' =[h`i',bA`i',seA`i',bB`i',seB`i',bC`i',seC`i',bD`i',seD`i']
		} 
	
	matrix a=[a5 \	a6 \	a7 \	a8 \	a9 \	a10 \	a11 \	a12 \	a13 \	a14 \	a15 \	a16 \	a17 \	a18 \	a19 \	a20 ]
		
	svmat a
	rename a1 h1
	label variable h1 "Bandwidth"
	
	rename a2 bA1
	rename a3 seA1
	g upA1 = bA1+1.96*seA1
	g lowA1 =  bA1-1.96*seA1
	
	rename a4 bB1
	rename a5 seB1
	g upB1 = bB1+1.96*seB1
	g lowB1 =  bB1-1.96*seB1
	
	rename a6 bC1
	rename a7 seC1
	g upC1 = bC1+1.96*seC1
	g lowC1 =  bC1-1.96*seC1
	
	rename a8 bD1
	rename a9 seD1
	g upD1 = bD1+1.96*seD1
	g lowD1 =  bD1-1.96*seD1
	
}
	
	line  bA1 upA1 lowA1 bD1 upD1 lowD1 h1, xline(`=cct')  title(`: variable label $y', size(medium) color(black)) graphregion(color(white))   ///
			lcolor (black black black  red red red) ///
			lstyle(solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot) ///
			legend(order(1 4 7) label(1 "Short")  label(4 "Long") label(4 "Long+year-FE") rows(1)  region(lcolor(white) lwidth(0)))

	graph save "results/leeetal1.gph" , replace
	

//dropping stage
scalar drop  _all 
 matrix drop _all
drop bw*

******************************
* 2) score democrat
******************************
global y score
label variable $y "RC(t)"
global run dgap
global run_reg dgap rdgap 

quietly {

 rdbwselect $y $run, kernel($kernel) 
 scalar cct=e(h_mserd)
	forval i = 5(1)20 {
	scalar h`i'=`i'*(0.065)/10
	g bw`i'=0
	replace bw`i'=1 if $run >-`=h`i'' & $run <`=h`i''
	} 
	
	forval i = 5(1)20 {
		reg $y d $run_reg if bw`i'==1, cluster(id) // model A black
			scalar bA`i'= _b[d]
			scalar seA`i'= _se[d]
		reg $y d m $run_reg if bw`i'==1, cluster(id) // model B red
			scalar bB`i'= _b[d]
			scalar seB`i'= _se[d]
		reg $y d m dm $run_reg if bw`i'==1, cluster(id) // model C green
			scalar bC`i'= _b[d]
			scalar seC`i'= _se[d]
		reg $y d m dcon* $run_reg if bw`i'==1, cluster(id) // model D  red
			scalar bD`i'= _b[d]
			scalar seD`i'= _se[d]
		
		matrix a`i' =[h`i',bA`i',seA`i',bB`i',seB`i',bC`i',seC`i',bD`i',seD`i']
		} 
	
	matrix a=[a5 \	a6 \	a7 \	a8 \	a9 \	a10 \	a11 \	a12 \	a13 \	a14 \	a15 \	a16 \	a17 \	a18 \	a19 \	a20 ]
		
	svmat a
	rename a1 h2
	label variable h2 "Bandwidth"
	
	rename a2 bA2
	rename a3 seA2
	g upA2 = bA2+1.96*seA2
	g lowA2 =  bA2-1.96*seA2
	
	rename a4 bB2
	rename a5 seB2
	g upB2 = bB2+1.96*seB2
	g lowB2 =  bB2-1.96*seB2
	
	rename a6 bC2
	rename a7 seC2
	g upC2 = bC2+1.96*seC2
	g lowC2 =  bC2-1.96*seC2
	
	rename a8 bD2
	rename a9 seD2
	g upD2 = bD2+1.96*seD2
	g lowD2 =  bD2-1.96*seD2
	
}
	
	line  bA2 upA2 lowA2  bD2 upD2 lowD2 h2, xline(`=cct')  title(`: variable label $y', size(medium) color(black)) graphregion(color(white))   ///
			lcolor (black black black  red red red) ///
			lstyle(solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot) ///
			legend(order(1 4 7) label(1 "Short")  label(4 "Long") label(4 "Long+year-FE") rows(1)  region(lcolor(white) lwidth(0)))

	graph save "results/leeetal2.gph" , replace			
			

//dropping stage
scalar drop  _all 
 matrix drop _all
drop bw*


******************************
* 3) democrat lagdemocrat
******************************
global y d
label variable $y "D(t+1)" 
global run lagdgap
global run_reg lagdgap lagrdgap 

quietly {

 rdbwselect $y $run, kernel($kernel) 
 scalar cct=e(h_mserd)
	forval i = 5(1)20 {
	scalar h`i'=`i'*(0.065)/10
	g bw`i'=0
	replace bw`i'=1 if $run >-`=h`i'' & $run <`=h`i''
	} 
	
	forval i = 5(1)20 {
		reg $y lagd $run_reg if bw`i'==1, cluster(id) // model A black
			scalar bA`i'= _b[lagd]
			scalar seA`i'= _se[lagd]
		reg $y lagd lagm $run_reg if bw`i'==1, cluster(id) // model B red
			scalar bB`i'= _b[lagd]
			scalar seB`i'= _se[lagd]
		reg $y lagd lagm lagdm $run_reg if bw`i'==1, cluster(id) // model C green
			scalar bC`i'= _b[lagd]
			scalar seC`i'= _se[lagd]
		reg $y lagd lagm dcon* $run_reg if bw`i'==1, cluster(id) // model D  red
			scalar bD`i'= _b[lagd]
			scalar seD`i'= _se[lagd]
		
		matrix a`i' =[h`i',bA`i',seA`i',bB`i',seB`i',bC`i',seC`i',bD`i',seD`i']
		} 
	
	matrix a=[a5 \	a6 \	a7 \	a8 \	a9 \	a10 \	a11 \	a12 \	a13 \	a14 \	a15 \	a16 \	a17 \	a18 \	a19 \	a20 ]
		
	svmat a
	rename a1 h3
	label variable h3 "Bandwidth"
	
	rename a2 bA3
	rename a3 seA3
	g upA3 = bA3+1.96*seA3
	g lowA3 =  bA3-1.96*seA3
	
	rename a4 bB3
	rename a5 seB3
	g upB3 = bB3+1.96*seB3
	g lowB3 =  bB3-1.96*seB3
	
	rename a6 bC3
	rename a7 seC3
	g upC3 = bC3+1.96*seC3
	g lowC3 =  bC3-1.96*seC3
	
	rename a8 bD3
	rename a9 seD3
	g upD3 = bD3+1.96*seD3
	g lowD3 =  bD3-1.96*seD3
	
}
	
	line  bA3 upA3 lowA3 bD3 upD3 lowD3 h3, xline(`=cct')  title(`: variable label $y', size(medium) color(black)) graphregion(color(white))   ///
			lcolor (black black black  red red red) ///
			lstyle(solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot) ///
			legend(order(1 4 7) label(1 "Short")  label(4 "Long") label(4 "Long+year-FE") rows(1)  region(lcolor(white) lwidth(0)))
	
	graph save "results/leeetal3.gph" , replace	

//dropping stage
scalar drop  _all 
 matrix drop _all
drop bw*


******************************
* 4) ELECT COMPONENT = 2)*3)
******************************
global run dgap
global lagrun lagdgap
global run_reg dgap rdgap 
global lagrun_reg lagdgap lagrdgap 

quietly {

	forval i = 5(1)20 {
	scalar h`i'=`i'*(0.065)/10
	g bw2_`i'=0
	g bw3_`i'=0
	replace bw2_`i'=1 if $run >-`=h`i'' & $run <`=h`i''
	replace bw3_`i'=1 if $lagrun >-`=h`i'' & $lagrun <`=h`i''
	} 
	
	forval i = 5(1)20 {
		reg score d $run_reg if bw2_`i'==1 // model A black
		est store A2
		reg d  lagd $lagrun_reg if bw3_`i'==1 // model A black
		est store A3
		suest A2 A3, vce(cluster id)
		nlcom [A2_mean]d * [A3_mean]lagd
		matrix beta=r(b)
		matrix varianza=r(V)
		scalar bA`i'=beta[1,1]
		scalar seA`i'=sqrt(varianza[1,1])
	
		reg score d m $run_reg if bw2_`i'==1 // model B black
		est store B2
		reg d  lagd lagm $lagrun_reg if bw3_`i'==1 // model B black
		est store B3
		suest B2 B3, vce(cluster id)
		nlcom [B2_mean]d * [B3_mean]lagd
		matrix beta=r(b)
		matrix varianza=r(V)
		scalar bB`i'=beta[1,1]
		scalar seB`i'=sqrt(varianza[1,1])
	
		reg score d m dm $run_reg if bw2_`i'==1 // model C RED
		est store C2
		reg d  lagd lagm lagdm $lagrun_reg if bw3_`i'==1 // model C RED
		est store C3
		suest C2 C3, vce(cluster id)
		nlcom [C2_mean]d * [C3_mean]lagd
		matrix beta=r(b)
		matrix varianza=r(V)
		scalar bC`i'=beta[1,1]
		scalar seC`i'=sqrt(varianza[1,1])
		
		reg score d m dcon* $run_reg if bw2_`i'==1 // model D black
		est store D2
		reg d  lagd lagm dcon* $lagrun_reg if bw3_`i'==1 // model D black
		est store D3
		suest D2 D3, vce(cluster id)
		nlcom [D2_mean]d * [D3_mean]lagd
		matrix beta=r(b)
		matrix varianza=r(V)
		scalar bD`i'=beta[1,1]
		scalar seD`i'=sqrt(varianza[1,1])
		
		matrix a`i' =[h`i',bA`i',seA`i',bB`i',seB`i',bC`i',seC`i',bD`i',seD`i']
		}
		
	matrix a=[a5 \	a6 \	a7 \	a8 \	a9 \	a10 \	a11 \	a12 \	a13 \	a14 \	a15 \	a16 \	a17 \	a18 \	a19 \	a20 ]
	

	svmat a
	rename a1 h4
	label variable h4 "Bandwidth"
	
	rename a2 bA4
	rename a3 seA4
	g upA4 = bA4+1.96*seA4
	g lowA4 =  bA4-1.96*seA4
	
	rename a4 bB4
	rename a5 seB4
	g upB4 = bB4+1.96*seB4
	g lowB4 =  bB4-1.96*seB4
	
	rename a6 bC4
	rename a7 seC4
	g upC4 = bC4+1.96*seC4
	g lowC4 =  bC4-1.96*seC4
	
	rename a8 bD4
	rename a9 seD4
	g upD4 = bD4+1.96*seD4
	g lowD4 =  bD4-1.96*seD4
}
	line  bA4 upA4 lowA4 bD4 upD4 lowD4 h4,   title("Elect component", size(medium) color(black)) graphregion(color(white))   ///
			lcolor (black black black  red red red) ///
			lstyle(solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot) ///
			legend(order(1 4 7) label(1 "Short")  label(4 "Long") label(4 "Long+year-FE") rows(1)  region(lcolor(white) lwidth(0)))

	graph save "results/leeetal4.gph" , replace

//dropping stage
scalar drop  _all 
 matrix drop _all
drop bw*


******************************
* 5) ELECT COMPONENT = 1)-4)
******************************
global run dgap
global lagrun lagdgap
global run_reg dgap rdgap 
global lagrun_reg lagdgap lagrdgap 


quietly {

	forval i = 5(1)20 {
	scalar h`i'=`i'*(0.065)/10
	g bw1_`i'=0
	g bw2_`i'=0
	g bw3_`i'=0
	replace bw1_`i'=1 if $lagrun >-`=h`i'' & $lagrun <`=h`i''
	replace bw2_`i'=1 if $run >-`=h`i'' & $run <`=h`i''
	replace bw3_`i'=1 if $lagrun >-`=h`i'' & $lagrun <`=h`i''
	} 
	
	forval i = 5(1)20 {
	    reg score  lagd $lagrun_reg if bw1_`i'==1 // model A black
		est store A1
		reg score d $run_reg if bw2_`i'==1 // model A black
		est store A2
		reg d  lagd $lagrun_reg if bw3_`i'==1 // model A black
		est store A3
		suest A1 A2 A3, vce(cluster id)
		nlcom [A1_mean]lagd - [A2_mean]d * [A3_mean]lagd
		matrix beta=r(b)
		matrix varianza=r(V)
		scalar bA`i'=beta[1,1]
		scalar seA`i'=sqrt(varianza[1,1])
	
		reg score  lagd lagm $lagrun_reg if bw1_`i'==1 // model B black
		est store B1
		reg score d m $run_reg if bw2_`i'==1 // model B black
		est store B2
		reg d  lagd lagm $lagrun_reg if bw3_`i'==1 // model B black
		est store B3
		suest B1 B2 B3, vce(cluster id)
		nlcom [B1_mean]lagd - [B2_mean]d * [B3_mean]lagd
		matrix beta=r(b)
		matrix varianza=r(V)
		scalar bB`i'=beta[1,1]
		scalar seB`i'=sqrt(varianza[1,1])
	
		reg score  lagd lagm lagdm $lagrun_reg if bw1_`i'==1 // model C RED
		est store C1
		reg score d m dm $run_reg if bw2_`i'==1 // model C RED
		est store C2
		reg d  lagd lagm lagdm $lagrun_reg if bw3_`i'==1 // model C RED
		est store C3
		suest C1 C2 C3, vce(cluster id)
		nlcom [C1_mean]lagd - [C2_mean]d * [C3_mean]lagd
		matrix beta=r(b)
		matrix varianza=r(V)
		scalar bC`i'=beta[1,1]
		scalar seC`i'=sqrt(varianza[1,1])
		
		reg score  lagd lagm dcon* $lagrun_reg if bw1_`i'==1 // model D black
		est store D1
		reg score d m dcon* $run_reg if bw2_`i'==1 // model D black
		est store D2
		reg d  lagd lagm dcon* $lagrun_reg if bw3_`i'==1 // model D black
		est store D3
		suest D1 D2 D3, vce(cluster id)
		nlcom [D1_mean]lagd - [D2_mean]d * [D3_mean]lagd
		matrix beta=r(b)
		matrix varianza=r(V)
		scalar bD`i'=beta[1,1]
		scalar seD`i'=sqrt(varianza[1,1])
		
		matrix a`i' =[h`i',bA`i',seA`i',bB`i',seB`i',bC`i',seC`i',bD`i',seD`i']
		}
		
	matrix a=[a5 \	a6 \	a7 \	a8 \	a9 \	a10 \	a11 \	a12 \	a13 \	a14 \	a15 \	a16 \	a17 \	a18 \	a19 \	a20 ]
	

	svmat a
	rename a1 h5
	label variable h5 "Bandwidth"
	
	rename a2 bA5
	rename a3 seA5
	g upA5 = bA5+1.96*seA5
	g lowA5 =  bA5-1.96*seA5
	
	rename a4 bB5
	rename a5 seB5
	g upB5 = bB5+1.96*seB5
	g lowB5 =  bB5-1.96*seB5
	
	rename a6 bC5
	rename a7 seC5
	g upC5 = bC5+1.96*seC5
	g lowC5 =  bC5-1.96*seC5
	
	rename a8 bD5
	rename a9 seD5
	g upD5 = bD5+1.96*seD5
	g lowD5 =  bD5-1.96*seD5
}

	line  bA5 upA5 lowA5  bD5 upD5 lowD5 h1,   title("Affect component", size(medium) color(black)) graphregion(color(white))   ///
			lcolor (black black black  red red red) ///
			lstyle(solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot solid shortdash_dot shortdash_dot) ///
			legend(order(1 4) label(1 "Short")   label(4 "Long+year-FE") rows(1)  region(lcolor(white) lwidth(0)))
	
	graph save "results/leeetal5.gph" , replace



******************************
* COMBINE
******************************
grc1leg results/leeetal1.gph results/leeetal2.gph results/leeetal3.gph results/leeetal4.gph results/leeetal5.gph ,  graphregion(color(white)) 
graph display, ysize(12) xsize(13)

// save multi-graph
graph export "results/leeetal.pdf", replace

// erase single graphs
foreach f in results/leeetal1.gph results/leeetal2.gph results/leeetal3.gph results/leeetal4.gph results/leeetal5.gph {
	cap erase "`f'"
}
